/*
 * Copyright (C), 2002-2017, 苏宁易购电子商务有限公司
 * FileName: ExchangeController.java
 * Author:   15050536
 * Date:     2017年5月4日 下午2:59:28
 * Description: //模块目的、功能描述      
 * History: //修改记录
 * <author>      <time>      <version>    <desc>
 * 修改人姓名             修改时间            版本号                  描述
 */
package com.suning.sawp.in.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.suning.sawp.constants.ErrorCodeConstants;
import com.suning.sawp.constants.LoggerConstants;
import com.suning.sawp.dto.common.QueryResult;
import com.suning.sawp.dto.common.ReturnMsg;
import com.suning.sawp.dto.exchange.CityInfoDto;
import com.suning.sawp.intf.exchange.ExchangeActivityService;
import com.suning.sawp.po.exchange.ExchangeActivity;
import com.suning.sawp.service.impl.promotion.CityTransServiceImpl;
import com.suning.sawp.service.util.DateUtils;

import net.sf.json.JSONObject;

/**
 * 已旧换新后台controller
 *
 * @author 15050536 石键平
 */
@Controller
@RequestMapping("/adminPage/exchange/")
public class ExchangeController extends DjBaseController {

    private static final Logger LOGGER = LoggerFactory.getLogger(LoggerConstants.SAWP_SERVICE);

    @Autowired
    private ExchangeActivityService exchangeActivityService;
    @Autowired
    private CityTransServiceImpl cityTransServiceImpl;

    @RequestMapping("queryActivityList")
    public MyGsonView queryActivityVo(String code, String type) {
        ReturnMsg<QueryResult<ExchangeActivity>> ret = new ReturnMsg<QueryResult<ExchangeActivity>>();
        try {
            QueryResult<ExchangeActivity> data = exchangeActivityService.queryActivityList(code, type);
            ret.setData(data);
        } catch (Exception e) {
            LOGGER.error("查询已旧换新活动列表异常", e);
            ret.setError(ErrorCodeConstants.ERR_0002);
            ret.setErrorMessage("查询已旧换新活动列表异常");
        }
        return setGsonView(ret);
    }

    @RequestMapping("saveRule")
    @ResponseBody
    public ReturnMsg<String> saveRule(ExchangeActivity activity, HttpServletRequest request) {
        // 前端页面传来的参数可能都为空

        String staffId = request.getRemoteUser();
        activity.setConfigEmployee(staffId);
        activity.setConfigTime(DateUtils.getDateStrByFormat(new Date(), null));
        if (StringUtils.isNotEmpty(activity.getOrderStartTime())) {
            String ost = activity.getOrderStartTime() + " 00:00:00";
            activity.setOrderStartTime(ost);
        } else {
            activity.setOrderStartTime(null);
        }
        if (StringUtils.isNotEmpty(activity.getOrderEndTime())) {
            String oet = activity.getOrderEndTime() + " 23:59:59";
            activity.setOrderEndTime(oet);
        } else {
            activity.setOrderEndTime(null);
        }

        // 前端传来选中的订单类别和品类
        String orderStatus[] = request.getParameterValues("orderStatus");
        String cateTypes[] = request.getParameterValues("cateType");

        ReturnMsg<String> returnMsg = exchangeActivityService.saveRule(activity, orderStatus, cateTypes);
        return returnMsg;
    }

    @RequestMapping("findActivityById")
    public MyGsonView findActivityById(String activityId) {
        ReturnMsg<JSONObject> returnMsg = exchangeActivityService.findActivityById(activityId);
        return setGsonView(returnMsg);
    }

    @RequestMapping("queryCityList")
    public MyGsonView queryCityList() {
        List<CityInfoDto> cityList = cityTransServiceImpl.queryCityInfoList();
        ReturnMsg<List<CityInfoDto>> returnMsg = new ReturnMsg<List<CityInfoDto>>();
        returnMsg.setData(cityList);
        return setGsonView(returnMsg);
    }
}
